<?php
/**
 * MySQL PDO 连接实例
 * User: lisgroup
 * Date: 2017/11/26
 * Time: 17:14
 */

namespace lisfw\DB;


class MysqlPDO implements DbInterface
{
    protected $pdo;
    protected $_pconnect;
    protected $mode;
    protected $fetchAction;

    private $get_mode;
    private $get_fetch_action;

    public function __construct($configKey = 'DB_CONFIG', $pconnect = false)
    {
        if (is_null($this->pdo)) {
            $dbConfig = config($configKey);
            $this->_pconnect = [\PDO::ATTR_PERSISTENT => $pconnect];
            $this->pdo_connect($dbConfig);
        }
    }

    /**
     * PDO 连接操作
     * @param $dbConfig
     */
    private function pdo_connect($dbConfig)
    {
        try {
            $dsn = 'mysql:host=' . $dbConfig['DB_HOST'] . ';dbname=' . $dbConfig['DB_NAME'];
            $this->pdo = new \PDO($dsn, $dbConfig['DB_USER'], $dbConfig['DB_PASSWORD'], $this->_pconnect);
        } catch (\Exception $exception) {
            $this->getExceptionError($exception->getMessage(), $exception->getLine(), $exception->getLine());
        }
    }

    /**
     * 输出具体的错误信息并结束代码
     * @param $getMessage
     * @param $getLine
     * @param $getFile
     */
    private function getExceptionError($getMessage, $getLine, $getFile)
    {
        echo 'Error: The Error Message is ' . $getMessage . '<br> The Error In Line ' . $getLine . '<br> The Error File is ' . $getFile;
        exit();
    }

    protected function getValueByFetchAction($sql, $fetchAction = 'assoc', $mode)
    {
        $this->mode = $this->getStyleMode($mode);
        $this->fetchAction = $this->fetchAction($fetchAction);
    }


    private function getStyleMode($styleMode)
    {
        switch ($styleMode) {
            case true:
                $this->get_mode = \PDO::CASE_UPPER;
                break;
            case false:
                $this->get_mode = \PDO::CASE_LOWER;
                break;
            default:
                $this->get_mode = \PDO::CASE_NATURAL;
                break;

        }
    }
    private function fetchAction($fetchAction)
    {
        switch ($fetchAction) {
            case 'assoc':
                $this->get_fetch_action = \PDO::FETCH_ASSOC;
                break;
            case 'num':
                $this->get_fetch_action = \PDO::FETCH_NUM;
                break;
            case 'object':
                $this->get_fetch_action = \PDO::FETCH_OBJ;
                break;
            case '':
                $this->get_fetch_action = \PDO::FETCH_BOTH;
                break;
            default :
                $this->get_fetch_action = \PDO::FETCH_ASSOC;
                break;

        }
        return $this->get_fetch_action;
    }

    //查询方法
    public function query($sql)
    {
    }

    //查询二维数组
    public function getAll($sql)
    {
    }

    //单个值
    public function getOne($sql)
    {
    }

    //一行
    public function getCol($sql)
    {
    }

    //一列
    public function getRow($sql)
    {
    }

    //关闭
    public function close()
    {
    }
}